package io.sdph.controller;

import io.sdph.products.IProductManager;
import io.sdph.products.exceptions.ProductException;
import io.sdph.products.model.Product;
import io.sdph.userprofile.ISessionManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


/**
 * Servlet odpowiedzialny za mozliwosc usuniecia produktu w panelu przedstawiciela handlowego (admin.jsp)
 *
 * @author MODUL FRONTU (WWW)
 */
@Controller
@RequestMapping("/admin/DeleteProduct")
public class AdminDeleteProduct {
       
        private static final String SELECTED_PRODUCT = "products";
        private static final String ANOTHER = "all";
       
        private static final String PAGE_VIEW = "admin";

        /** Komponent pomocniczy do zarz�dzania sesj� u�ytkownika. */
        @Autowired
        ISessionManager sessionManager;
        
        @Autowired
        IProductManager productManager;
       
        //HttpServletRequest arg0, HttpServletResponse arg1
        @RequestMapping(method=RequestMethod.GET)
        public ModelAndView showPage() throws Exception {
               
                ModelAndView model = new ModelAndView(PAGE_VIEW);
                model.addObject("pageName", "products");
                return model;
        }
       
        @RequestMapping(method=RequestMethod.POST)
        public ModelAndView adminDeleteProduct(
                        @RequestParam(SELECTED_PRODUCT) final String selectedProduct                    
        				) {
               
                ModelAndView model = new ModelAndView(PAGE_VIEW);
                try {
					productManager.deleteProduct(new Product(selectedProduct));
				} catch (ProductException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                model.addObject("pageName", "products");
                return model;
        }
       
}
